﻿function Load_Roles() {
    CallWebService(adminRolesGetUrl, null,
                function (result) {
                    var response = JSON.parse(result.d);
                    if (response.IsSuccess == false) {
                        ShowErrorMessage(response);
                    } else {
                        var tbl = "<table id='rolesDataTable' class='Striped'>";
                        tbl += "<thead><tr><th style='width:150px'/><th>Role</th><th>Description</th></tr></thead>";
                        tbl += "<tbody>";
                        $.each(response.Payload, function (index) {
                            tbl += "<tr>";
                            tbl += "<td><input id='btnRoleEdit_" + index + "' class='EditRole' type='button' value='Edit' index='" + index + "'/>&nbsp";
                            tbl += "<input id='btnRoleDelete_" + index + "' class='DeleteRole' type='button' index='" + index + "' value='Delete'/>&nbsp";
                            tbl += "<input id='btnRoleSave_" + index + "' class='SaveRole' type='button' index='" + index + "' value='Save'  style='display:none'/></td>";
                            tbl += "<td>" + "<span id= 'rd_Role_Name_" + index + "' roleId='" + this.RoleId + "'>" + this.RoleName + "</span>" +
                            "<input id='ed_Role_Name_" + index + "' type='text' value='" + this.RoleName + "' style='display:none;width:100%'/>" +
                            "</td>";
                            tbl += "<td>" + "<span id= 'rd_Role_Desc_" + index + "' roleId='" + this.RoleId + "'>" + this.Description + "</span>" +
                            "<input id='ed_Role_Desc_" + index + "' type='text' value='" + this.Description + "' style='display:none;width:100%'/>" +
                            "</td>";
                            tbl += "</tr>";
                        });
                        tbl += "</tbody>";
                        tbl += "</table>";
                        tbl += "<input id='NewRole' type='button' value='New'/>";
                        $("#RolesTable").html(tbl);
                        // Add striping
                        $("#rolesDataTable tr:nth-child(even)").addClass("alt");
                        $("#rolesDataTable tr:nth-child(odd)").addClass("alt2");
                        $("#rolesDataTable tr:gt(0)").mouseenter(function () { $(this).addClass("over"); }).mouseleave(function () { $(this).removeClass("over"); });
                        // Delete Role
                        $(".DeleteRole").button().click(function () {
                            if (confirm("Do you want to delete? Note: Delete will fail if there are users attached to this role.") == true) {
                                var input = { "roleId": $(('#rd_Role_Name_' + $(this).attr('index'))).attr("RoleId") };
                                CallWebService(adminRolesDeleteUrl, input, function (result) {
                                    var response = JSON.parse(result.d);
                                    if (response.IsSuccess == false) {
                                        ShowErrorMessage(response);
                                    }
                                    Load_Roles();
                                });
                            }
                        });
                        // Edit Role Button
                        $(".EditRole").button().click(function () {
                            switch ($(this).val()) {
                                case "Edit":
                                    $(this).val("Cancel");
                                    $(("#ed_Role_Name_" + $(this).attr('index'))).show();
                                    $(("#ed_Role_Desc_" + $(this).attr('index'))).show();
                                    $(("#rd_Role_Name_" + $(this).attr('index'))).hide();
                                    $(("#rd_Role_Desc_" + $(this).attr('index'))).hide();
                                    $(("#btnRoleDelete_" + $(this).attr('index'))).hide();
                                    $(("#btnRoleSave_" + $(this).attr('index'))).show();
                                    break;
                                default:
                                    $(this).val("Edit");
                                    $(("#ed_Role_Name_" + $(this).attr('index'))).hide();
                                    $(("#ed_Role_Desc_" + $(this).attr('index'))).hide();
                                    $(("#rd_Role_Name_" + $(this).attr('index'))).show();
                                    $(("#rd_Role_Desc_" + $(this).attr('index'))).show();
                                    $(("#btnRoleDelete_" + $(this).attr('index'))).show();
                                    $(("#btnRoleSave_" + $(this).attr('index'))).hide();
                                    break;
                            }
                        });
                        // Update Role
                        $(".SaveRole").button().click(function () {
                            var inp = { "roleId": $(('#rd_Role_Name_' + $(this).attr('index'))).attr("RoleId"),
                                "roleName": $(("#ed_Role_Name_" + $(this).attr('index'))).val(), "description": $(("#ed_Role_Desc_" + $(this).attr('index'))).val()
                            };
                            CallWebService(adminRolesSaveUrl, inp, function (result) {
                                if (JSON.parse(result.d).IsSuccess == false) {
                                    alert("Save Failed.");
                                }
                            });
                            $(this).hide();
                            $(("#btnRoleDelete_" + $(this).attr('index'))).show();
                            $(("#btnRoleEdit_" + $(this).attr('index'))).show().val("Edit"); ;
                            $(("#rd_Role_Name_" + $(this).attr('index'))).show();
                            $(("#rd_Role_Desc_" + $(this).attr('index'))).show();
                            $(("#ed_Role_Name_" + $(this).attr('index'))).hide();
                            $(("#ed_Role_Desc_" + $(this).attr('index'))).hide();
                            Load_Roles();
                        });
                        // Add Role
                        $("#NewRole").button().click(function () {
                            $("#NewRole").attr("disabled", true); 
                            $("#rolesDataTable > tbody:last").append("<tr id='RoleAddRow'><td><input id='btnAddRole' type='button' value='Add'/>" +
                            "<input id='btnCancelAddRole' type='button' value='Cancel'/></td><td><input id='ed_Role_Name' style='width:100%' type='text'/></td>" +
                            "<td><input type='text' id='ed_Role_Desc' style='width:100%'/></td></tr>");
                            $("#btnCancelAddRole").button().click(function () { $('#RoleAddRow').remove(); $("#NewRole").attr("disabled", false); });
                            $("#btnAddRole").button().click(function () {
                                $("#NewRole").attr("disabled", false);
                                var inp = { "roleId": "", "roleName": $("#ed_Role_Name").val(), "description": $("#ed_Role_Desc").val() };
                                CallWebService(adminRolesSaveUrl, inp, function (result) {
                                    var response = JSON.parse(result.d);
                                    if (response.IsSuccess == false) {
                                        ShowErrorMessage(response);
                                    }
                                    Load_Roles();
                                });
                                $('#RoleAddRow').remove();
                            });
                        });
                    }
                });
}